import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.*;






/* Ori Perl
 * 12/2/10
 * Software Design pd. 8
 * This program plays a single player version of Mr. Stein's Unfair Game
 * A certain number of questions are loaded from a file and the player gets -1 if the answer is wrong and 1 if the
 * question is right. A number is generated from -1950 to 2000 and is multiplied by the number and is added to the
 * player's score.
 * 
 * */
public class Game extends JPanel{
	private MediaTracker tracker;
	private AnimThread anim;
	boolean submitted = false;
	int score;
	int time;
	int qNum = 5;
	int order = 0;
	String submission;
	Boolean answered;
	ArrayList <String>  questions;
	ArrayList <String> answers;
	JLabel prompt,tCount,sCount,response;
	JTextField answer;
	JButton submit;
	
	public static void main(String args[]) {
		final Game smash = new Game();
		JFrame app = new JFrame();
		app.add(smash, BorderLayout.CENTER);
		app.setSize(1024,800);
		app.setVisible(true);
		app.setResizable(false);
	}//end main
	
	public Game(){
		Font f = new Font("Century Gothic",Font.PLAIN, 24);
		setFont(f);
		setLayout(new GridLayout(3,3));
		time = 30;
		submission = new String();
		answered = false;
		score = 0;
		questions = new ArrayList <String>();
		answers = new ArrayList <String>();
		setSize(1024,800);
		tracker = new MediaTracker(this);
		answer = new JTextField();
		answer.setFont(f);
		answer.setSize(200, 100);
		submit = new JButton("Submit Answer");
		submit.setFont(f);
		anim = new AnimThread();
		try{
			Scanner console=new Scanner(new FileReader("easyQ.txt"));
			int qMax = console.nextInt();
			console.nextLine();
			Set<Integer> qFinder = new TreeSet <Integer>();
			//randomly finds 5 questions
			int rand= 0;
			while(qFinder.size()<qMax){
				//find a random question
				rand = (int)(Math.random()*qMax)+1;
				//tries to add it to the set
				qFinder.add(rand);
			}//end for 
			Iterator <Integer>qIndex = qFinder.iterator();
			int count = 0;
			while (qIndex.hasNext()){
				int num =qIndex.next().intValue();
				for (int i= count+1;  i<num; i++){
					console.nextLine();
					console.nextLine();
					count++;
				}//end for
				int rand1 = (int)Math.random()*questions.size();
				questions.add(0, console.nextLine());
				answers.add(0, console.nextLine());
				count++;
			}//end for
		}//end try
		catch(FileNotFoundException e){
	    	System.out.println("file not found");
	    }//end catch error
		prompt = new JLabel(questions.get(0));
		prompt.setFont(f);
		sCount = new JLabel("Score: " + score);
		sCount.setFont(f);
		tCount = new JLabel("Time: " + time);
		tCount.setFont(f);
		response = new JLabel("Answer the question");
		response.setFont(f);
		//fills empty space
		this.add(response);
		this.add(prompt);
		this.add(tCount);
		this.add(new JLabel());
		this.add(answer);
		this.add(new JLabel());
		this.add(new JLabel());
		this.add(submit);
		this.add(sCount);
		submit.addActionListener(new ButtonAction()); 
		anim.start();
	}//end default constructor
	
	private class ButtonAction implements ActionListener {
		// When the button is clicked this method will get automatically called 
			public void actionPerformed(ActionEvent evt){ 
								// Check which component called this method 
				if (evt.getSource() == submit){
					if (time >0 && !submitted){
						submitted = true;
						submission = answer.getText().toLowerCase().trim();
						answered = true;
						int rand = (int)(Math.random()*3951)-1950;
						if (answers.get(order).equals(submission)){
							response.setText("Correct!\n Your score " + score + " + " + rand);
							score += rand;
						}//end if
						else if (submission.equals("millard fillmore")){
							response.setText("Why that answer?!\n Your score " + score + " + " + rand + " * .5");
							score += rand *.5;
						}//end else if
						else {
							response.setText("Wrong!\n Your score " + score + " + " + rand + "* -1");
							score -= rand;
						}//end else
						//next question
						time = 3;
						sCount.setText("Score: " +  score);
						order++;
					}//end if
				}//end if
			}//end action performed
	}//end button action

	class AnimThread extends Thread {
		public void run() {
			while (Thread.currentThread() == anim) {
				try {
					sleep(1000);
				}//end try 
				catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//end catch
				//check for 
				if (time>0 ){
					time--;
						tCount.setText("Time: " + time);
				}//end if
				else{
					tCount.setText("");
					if (order == qNum){
						System.out.println(score);
						System.exit(0);
					}
					submitted =false;
					time = 30;
					response.setText("Answer the question");
					prompt.setText(questions.get(order));
					if (!submitted){
						
					}//end if
				}//end else
			}//end while
		}//end run	
	}//end anim thread
}//end game